<?php
if( !defined('BASEPATH') ) exit ( 'No direct script access allowed' );

class Menu extends admin_controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$sql = $this->db->order_by('id', 'desc')->get('menu')->result_array();

		$this->template->title('Quản lý Menu')
			->set('body', $sql)
			->set('link_add', URL_ADMIN.'menu/add')
			->set('link_edit', URL_ADMIN.'menu/edit/')
			->set('link_del', URL_ADMIN.'menu/delete/')
			->build('admin/menu/index');
	}

	public function add()
	{

		if($_POST)
		{
			//print_r($_POST);
			$this->form_validation->set_rules('name', 'Tên Menu', 'required');

			if($this->form_validation->run() == TRUE)
			{
				if($this->input->post('link') !== "") {
					$link = $this->input->post('link');
					$link_cat = "";
				} else {
					$link = "";
					$link_cat = $this->input->post('link_cat');
				}

				$arr = array(
					'men_name'		=> $this->input->post('name'),
					'status'		=> (int)$this->input->post('status'),
					'order'			=> (int)$this->input->post('order'),
					'link'			=> $link,
					'link_cat'		=> $link_cat
				);

				if( $this->db->insert('menu', $arr) )
				{
					$this->message('info', 'Added successfuly');
					redirect(URL_ADMIN.'menu');
				} else {
					$this->message('info', 'Added faild');
					redirect(URL_ADMIN.'menu');
				}
			}

		}

		$this->db->order_by('order', 'asc');
		$cates = $this->db->get_where('category', array('type' => 'article'))->result_array();

		$this->template->title('Thêm menu')
			->set('link_action', URL_ADMIN.'menu/add')
			->set('link_cancel', URL_ADMIN.'menu')
			->set('cates', $cates)
			->build('admin/menu/add');

	}

	public function edit()
	{
		$id = $this->uri->segment(4);

		if($_POST)
		{
			$this->form_validation->set_rules('name', 'Tên menu', 'required');


			if($this->form_validation->run() == TRUE) {

				if($this->input->post('link') !== "") {
					$link = $this->input->post('link');
					$link_cat = "";
				} else {
					$link = "";
					$link_cat = $this->input->post('link_cat');
				}

				$arr = array(
					'men_name'		=> $this->input->post('name'),
					'status'		=> (int)$this->input->post('status'),
					'order'			=> (int)$this->input->post('order'),
					'link'			=> $link,
					'link_cat'		=> $link_cat
				);


				if( $this->db->update('menu', $arr, array( 'id' => $id )) )
				{
					$this->message('info', 'Updated successfuly');
					redirect(URL_ADMIN.'menu');
				} else {
					$this->message('info', 'Updated faild');
					redirect(URL_ADMIN.'menu');
				}
			}

		}

		$items = $this->db->get_where('menu', array('id' => $id))->row_array();

		$this->db->order_by('order', 'asc');
		$cates = $this->db->get_where('category', array('type' => 'article'))->result_array();

		$this->template->title('Sửa menu')
			->set('link_action', URL_ADMIN.'menu/edit/'. $id)
			->set('link_cancel', URL_ADMIN.'menu')
			->set('items', $items)
			->set('cates', $cates)
			->build('admin/menu/edit');
	}

	public function delete()
	{
		$id = $this->uri->segment(4);

		if ( $this->db->delete('menu', array( 'id' => $id )) )
		{
			$this->message('info', 'Deleted successfuly');
			redirect(URL_ADMIN.'menu');
		} else {
			$this->message('info', 'Deleted successfuly');
			redirect(URL_ADMIN.'menu');
		}

	}
}

/* End file */
/* Local: ./application/controller/admin/page.php */